#include<stdio.h>
#include<stdlib.h>
#define printx(x) printf("%8s\t",x)
//using namespace std;
struct data {
	int no;
	char name[10];
	int pno;
};
struct sqList {
	struct data stu;
	struct sqList *next;
};
sqList *head = NULL, *p = NULL;
int initSqlist()
{
	if (head == NULL) {
		head = (sqList*) malloc(sizeof(sqList));
		return 1;
	}
	return 0;
}
sqList * getInfo()
{
	sqList *q = (sqList*) malloc(sizeof(q));
	printx("请输入学号：\n");
	scanf("%d", &q->stu.no);
	printx("请输入姓名：\n");
	scanf("%s", q->stu.name);
	printx("请输入手机号码：\n");
	scanf("%d", &q->stu.pno);
	return q;
}

void showInfo(sqList *r)
{
	printf("%d\t", r->stu.no);
	printf("%s\t", r->stu.name);
	printf("%d\t", r->stu.pno);
}
int addSqList()
{
	sqList *r = getInfo();

	if (head == NULL) {
		initSqlist();

		head->next = r;
		r->next = NULL;
	} else {
		p = head;
		while (p->next) {
			p = p->next;
		}
		p->next = r;
		r->next = NULL;
//		p->next = r;
//		r->next = NULL;
//		p = r;
	}
	return 1;
};
sqList* search()
{
	sqList *q;
	q = head;
	int no;
	printx("请输入要查询人的学号：\n");
	scanf("%d", &no);
	while (q->next) {
		if (q->next->stu.no == no) {
			printf("找到数据：");
			showInfo(q->next);
			printf("\n");
			return q;
		}
		q = q->next;
	}
	return NULL;

}
int deleteSqList()
{

	sqList *r = search();
	if (!r) {
		return 0;
	}
	sqList *q;
	q = r->next;
	r->next = q->next;
	free(q);
	return 1;

};
int changeSqList()
{
	sqList *r = search();
	r->next = getInfo();
	return 1;

};
int	showSqList()
{
	printx("学号");
	printx("姓名");
	printx("手机号码");
	sqList *r = head;
	while (r->next) {
		printf("\n");
		showInfo(r->next);
		r = r->next;
	}
	return 1;

};


int main()
{
	int op = 0;

	while (1) {
		printf("\n");
		printx("STUDENT SYSTEM MENU");
		printx("1.添加学生信息");//add
		printx("2.删除学生信息");//delete
		printx("3.更改学生信息");//change
		printx("4.查看所有学生信息");//show
		printx("0.退出系统");
		scanf("%d", &op);
		system("cls");	
		switch (op) {
			case 1:
				printf("\n");

				addSqList();
				break;
			case 2:
				printf("\n");
				deleteSqList();
				break;
			case 3:
				printf("\n");
				changeSqList();
				break;
			case 4:
				printf("\n");
				showSqList();
				break;
			case 0:
				printx("即将退出");
				exit(0);
				break;
		}
	}
}
